<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 订单列表
     * @ApiTitle    (订单列表)
     * @ApiSummary  (订单列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/recycleLists)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiHeaders  (name=limit, type=int, required=true, description="条数")
     * @ApiHeaders  (name=page, type=int, required=true, description="页码")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function recycleLists(){
        $limit = $_REQUEST['limit']??10;
        $page = $_REQUEST['page']??1;
        $token = $_REQUEST['token']??'';
        if (empty($token)){
            $this->error("请去登录",1002);
        }
        $user_id = Db::name("user_token")->where("token",$token)->value("user_id");
        $lists = Db::name("order")
            ->where("user_id",$user_id)
            ->paginate($limit)
            ->toArray();
        if ($lists['data']){
            $this->success('操作成功',$lists);
        }else{
            $this->success('暂无数据');
        }
    }
    /**
     * 评价列表
     * @ApiTitle    (评价列表)
     * @ApiSummary  (评价列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/estimateLists)
     * @ApiHeaders  (name=limit, type=int, required=true, description="条数")
     * @ApiHeaders  (name=page, type=int, required=true, description="页码")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function estimateLists(){
        $limit = $_REQUEST['limit']??10;
        $page = $_REQUEST['page']??1;

        $lists = Db::name("estimate")
            ->alias("e")
            ->join("user u","u.id=e.user_id")
            ->field("u.nickname,u.avatar,e.*")
            ->where('e.status',1)
            ->order("e.createtime desc")
            ->paginate($limit)
            ->toArray();
        if ($lists['data']){
            foreach ($lists['data'] as &$value){
                $value['time'] = date("Y-m-d H:i:s",$value['createtime']);
            }
            $this->success('操作成功',$lists);
        }else{
            $this->success('暂无数据');
        }
    }

    public function getrule(){

        $res1 = Db::name("selfrule")->order("order asc")->select();
        $res2 = Db::name("lunbo")->find();
//        $res2 = Db::name("selfrule")->where("leixing",'常见问题')->select();
        $data['rule'] = $res1;
        $data['lunbo'] = $res2;
        $this->success('',$data);
    }
    /**
     * 获取tab分类
     * @ApiTitle    (获取tab分类)
     * @ApiSummary  (获取tab分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/getCates)
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function getCatesLists(){
        $lists = Db::name("cate")
            ->where("pid",0)
            ->select();

        if ($lists){
            $this->success('操作成功',$lists);
        }else{
            $this->success('暂无数据');
        }
    }

    /**
     * 获取分类下的分类
     * @ApiTitle    (获取分类下的分类)
     * @ApiSummary  (获取分类下的分类)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/getCateChild)
     * @ApiHeaders  (name=id, type=int, required=true, description="分类id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function getCateChild(){
        $id = $_REQUEST['pid']??0;
        if (!$id){
            $this->error("操作失败");
        }

        $lists = Db::name("cate")
            ->where("pid",$id)
            ->select();
        foreach ($lists as &$value){
            $child = Db::name("cate")->where('pid',$value['id'])->select();
            $value['list'] = $child;
        }
        if ($lists){
            $this->success('操作成功',$lists);
        }else{
            $this->success('暂无数据');
        }
    }
    /**
     * 获取试题列表
     * @ApiTitle    (获取试题列表)
     * @ApiSummary  (获取试题列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/getTiLists)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiHeaders  (name=limit, type=int, required=true, description="条数")
     * @ApiHeaders  (name=page, type=int, required=true, description="页码")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function getTiLists(){
        $brand_id = $_REQUEST['brand_id']??'';
        $list = Db::name("brand_tiku_relation")
            ->where("brand_id",$brand_id)
            ->find();
        if (!$list){
            //查找父级是否存在
            $lastpid1 = Db::name("cate")->where("id",$brand_id)->value("pid");
            $list = Db::name("brand_tiku_relation")
                ->where("brand_id",$lastpid1)
                ->find();
            if (!$list){
                //查找最上级
                $lastpid2 = Db::name("cate")->where("id",$lastpid1)->value("pid");
                $list = Db::name("brand_tiku_relation")
                    ->where("brand_id",$lastpid2)
                    ->find();
            }
        }
        if ($list){
            //获取题库的试题
            $questions = Db::name("question")->where("cate_id",$list['tiku_id'])->field("id,title as question,type")->order("weigh desc")->select();
            foreach ($questions as &$value){
                $answer = Db::name("option")->where("question_id",$value['id'])->field("id,content,order")->order("order asc")->select();
                $value['answers'] = $answer;
            }
            $this->success('操作成功',$questions);
            
        }else{
            $this->success('请先配置该产品的题库');
        }
    }

    /**
     * 评估算法
     * @ApiTitle    (评估算法)
     * @ApiSummary  (评估算法)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/assess)
     * @ApiHeaders  (name=data, type=int, required=true, description="json数组 {{"option":1},{"option":1}}")
     * @ApiHeaders  (name=id, type=int, required=true, description="机型id")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function assess(){
        $data = $_REQUEST['data']??'';
        $id = $_REQUEST['id']??'';
        if (!$data || !$id){
            $this->error("操作失败");
        }
        //评估详情
        $datadetail =[];
        //获取机型理想情况下的价格
        $price = Db::name("cate")->where("id",$id)->value("price");
        $new = json_decode($data,1);
        foreach ($new as &$value){
            $option = Db::name("option")->where("id",$value['option'])->field('content,price,question_id')->find();
            $question = Db::name("question")->where("id",$option['question_id'])->field('title,type')->find();
            $price -= $option['price'];
            $data1['title'] = $question['title'];
            $data1['content'] = $option['content'];
            $data1['type'] = $question['type'];
            $datadetail[] = $data1;
        }
        $res['price'] = $price;
        $res['datadetail'] = $datadetail;
        $this->success('获取成功',$res);
    }

    /**
     * 公司信息
     * @ApiTitle    (公司信息)
     * @ApiSummary  (公司信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Index/con)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="content", type="text", required=false, description="评价内容")
     * @ApiParams   (name="level", type="string", required=false, description="等级")
     * @ApiParams   (name="order_id", type="int", required=false, description="订单id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    })
     */
    public function con()
    {
        $list = Db::name("con")->find();
        if ($list){
            $this->success('操作成功',$list);
        }else{
            $this->error('操作失败');
        }

    }

    //获取本机型号的首页列表
    public function  getIndexMobile(){
        $str1 = $_REQUEST['model']??'';
        $str2 = explode('<',$str1);
        $str = $str2[0];
        if ($str){
            $str = trim($str); //清除字符串两边的空格
            $str = preg_replace("/\t/","",$str); //使用正则表达式替换内容，如：空格，换行，并将替换为空。
            $str = preg_replace("/\r\n/","",$str);
            $str = preg_replace("/\r/","",$str);
            $str = preg_replace("/\n/","",$str);
            $str = preg_replace("/ /","",$str);
            $str = preg_replace("/  /","",$str);  //匹配html中的空格
            $str = strtolower($str);
            $sql1 = 'SELECT id,image,price,cate_name, LOWER(model) as model FROM `re_cate`';
            $list = Db::query($sql1);
            $arr=[];
            foreach ($list as &$value){
                $str1 = $value['model'];
                $str1 = trim($str1); //清除字符串两边的空格
                $str1 = preg_replace("/\t/","",$str1); //使用正则表达式替换内容，如：空格，换行，并将替换为空。
                $str1 = preg_replace("/\r\n/","",$str1);
                $str1 = preg_replace("/\r/","",$str1);
                $str1 = preg_replace("/\n/","",$str1);
                $str1 = preg_replace("/ /","",$str1);
                $str1 = preg_replace("/  /","",$str1);  //匹配html中的空格
                $str1 = strtolower($str1);
                if ($str1 == $str){
                    $arr = $value;
                    break;
                }
            }
            if ($arr){
                $this->success('操作成功',$arr);
            }else{
                $this->error('该型号后台暂未配置',[]);
            }

        }

    }
}
